﻿using HealthcareSystem.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WeekExam.PassPort.Infrastructure.Interface;

namespace WeekExam.PassPort.Infrastructure.Implement
{
    /// <summary>
    /// 基础仓储基类实现
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }

        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        public async Task<T> GetByAsync(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().FirstOrDefaultAsync(expression);
        }
        public async Task<int> AddAsync(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }
        public async Task<int> UpdAsync(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
    }
}
